package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-06
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //根据计划id查询护理项目信息
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto, Long userId)
    {
        //保存护理计划
        NursingPlan nursingPlan = new NursingPlan();
        //属性拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //保存护理项目
        //int count = nursingProjectMapper.batchInsert(nursingPlanDto.getProjectPlans(),nursingPlan.getId());
        //获取护理项目对象
        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        //使用CollectionUtils工具类中的isEmpty方法 判断集合内是否为空
        if (!CollectionUtils.isEmpty(nursingProjectPlan)) {
            //不为空 就遍历循环它
            for (NursingProjectPlan projectPlan : nursingProjectPlan) {
                //手动塞入 创建时间
                projectPlan.setCreateTime(DateUtils.getNowDate());
                //塞入护理项目id
                projectPlan.setPlanId(nursingPlan.getId());
                projectPlan.setCreateBy(userId + "");
                //往表里插入
                nursingProjectPlanMapper.insertNursingProjectPlan(projectPlan);
            }
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //关联表: 先删除 再新增     计划表 : 直接修改
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        nursingPlan.setUpdateTime(new Date());
        nursingPlan.setUpdateBy(SecurityUtils.getUserId()+"");

        nursingPlanMapper.updateNursingPlan(nursingPlan);

        //前端穿过的数据,如果不为空表示修改操作,如果为空则修改状态
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            //先删除关联计划表
            nursingProjectPlanMapper.deleteById(nursingPlan. getId());
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setCreateTime(DateUtils.getNowDate());
                projectPlan.setPlanId(nursingPlan.getId());
                projectPlan.setCreateBy(SecurityUtils.getUserId()+"");
                nursingProjectPlanMapper.insertNursingProjectPlan(projectPlan);
            }
        }
        return 1 ;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        nursingProjectPlanMapper.deleteById(id);
        return nursingPlanMapper.deleteNursingPlanById(id);
    }

    /**
     * 查询所有计划
     * @return
     */
    @Override
    public List<NursingPlan> getNursingPlanAll() {
        return nursingPlanMapper.getNursingPlanAll();
    }
}
